using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class TimelineItemContainer : UserControl
	{
		private const string TAG = "TimelineItemContainer";
		private static List<TimelineItemContainer> sAllCurrInstance = new List<TimelineItemContainer>();
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(object), typeof(TimelineItemContainer), new PropertyMetadata(null, new PropertyChangedCallback(TimelineItemContainer.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		public static AlbumCoverCtrl sAlbumCoverCtrl;
		public static bool sBIsAlblumCoverLoaded = false;
		private object mObjectLocker = new object();
		private int mNTimeLineItemId = -1;
		internal Grid gridContainer;
		private bool _contentLoaded;
		public object ItemObject
		{
			get
			{
				return base.GetValue(TimelineItemContainer.ItemObjectProperty);
			}
			set
			{
				base.SetValue(TimelineItemContainer.ItemObjectProperty, value);
			}
		}
		public TimelineItemContainer()
		{
			TimelineItemContainer.addOneInctance(this);
			this.InitializeComponent();
			this.getCtrlItem();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~TimelineItemContainer()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		public static void CacheAllItems()
		{
			using (List<TimelineItemContainer>.Enumerator enumerator = TimelineItemContainer.sAllCurrInstance.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					TimelineItemContainer current = enumerator.get_Current();
					current.removeCtrl();
				}
			}
			TimelineItemContainer.sAllCurrInstance.Clear();
		}
		private static void addOneInctance(TimelineItemContainer itemCon)
		{
			TimelineItemContainer.sAllCurrInstance.Add(itemCon);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == TimelineItemContainer.ItemObjectProperty)
			{
				(dependencyObject_0 as TimelineItemContainer).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (dependencyPropertyChangedEventArgs_0.get_OldValue() != null)
				{
					SnsInfo snsInfo = dependencyPropertyChangedEventArgs_0.get_OldValue() as SnsInfo;
					if (snsInfo != null && snsInfo != null)
					{
						snsInfo.strObjectID == CircleMainPage.cStrAblumCoverID;
					}
				}
				if (this.ItemObject != null)
				{
					CircleMainPage.NotifyIndex(this.ItemObject);
					SnsInfo snsInfo2 = this.ItemObject as SnsInfo;
					if (snsInfo2 != null && snsInfo2.strObjectID == CircleMainPage.cStrAblumCoverID)
					{
						this.showAblumCover();
					}
					else
					{
						if (snsInfo2 != null && snsInfo2.strObjectID == SnsInfo.toStrID(0uL))
						{
							this.removeCtrl();
							FrameworkElement listTailCtrl = CircleMainPage.GetListTailCtrl();
							if (listTailCtrl != null)
							{
								if (listTailCtrl.get_Parent() != null)
								{
									(listTailCtrl.get_Parent() as Panel).get_Children().Remove(listTailCtrl);
								}
								this.gridContainer.get_Children().Add(listTailCtrl);
							}
						}
						else
						{
							this.setCtrlItem();
						}
					}
				}
				else
				{
					this.removeCtrl();
				}
			}
			if (dependencyPropertyChangedEventArgs_0.get_NewValue() == null)
			{
				this.removeCtrl();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			SnsInfo snsInfo = this.ItemObject as SnsInfo;
			if (snsInfo != null && snsInfo != null && snsInfo.strObjectID == CircleMainPage.cStrAblumCoverID && (TimelineItemContainer.sAlbumCoverCtrl == null || TimelineItemContainer.sAlbumCoverCtrl.get_Parent() != this.gridContainer))
			{
				this.showAblumCover();
			}
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		public void OnVisible()
		{
			if (this.gridContainer.get_Children().get_Count() == 1 && this.gridContainer.get_Children().get_Item(0) is TimelineItem)
			{
				TimelineItem timelineItem = this.gridContainer.get_Children().get_Item(0) as TimelineItem;
				if (timelineItem != null)
				{
					timelineItem.OnVisible();
				}
			}
		}
		private void RefreshItem()
		{
		}
		private void showAblumCover()
		{
			if (TimelineItemContainer.sAlbumCoverCtrl != null && (TimelineItemContainer.sAlbumCoverCtrl.get_Parent() != null || TimelineItemContainer.sAlbumCoverCtrl.mContact == null || TimelineItemContainer.sAlbumCoverCtrl.mContact.strUsrName != AccountMgr.strUsrName))
			{
				TimelineItemContainer.sAlbumCoverCtrl.remove_Loaded(new RoutedEventHandler(TimelineItemContainer.sAlbumCoverCtrl_Loaded));
				TimelineItemContainer.sAlbumCoverCtrl.remove_Unloaded(new RoutedEventHandler(TimelineItemContainer.sAlbumCoverCtrl_Unloaded));
				TimelineItemContainer.sAlbumCoverCtrl.AlbumCoverImg.set_Source(null);
				TimelineItemContainer.sAlbumCoverCtrl.unRegisterEvent();
				TimelineItemContainer.sAlbumCoverCtrl = null;
			}
			if (TimelineItemContainer.sAlbumCoverCtrl == null)
			{
				TimelineItemContainer.sAlbumCoverCtrl = new AlbumCoverCtrl(StorageMgr.contact.method_0(AccountMgr.strUsrName), true);
				TimelineItemContainer.sAlbumCoverCtrl.add_Loaded(new RoutedEventHandler(TimelineItemContainer.sAlbumCoverCtrl_Loaded));
				TimelineItemContainer.sAlbumCoverCtrl.add_Unloaded(new RoutedEventHandler(TimelineItemContainer.sAlbumCoverCtrl_Unloaded));
			}
			if (TimelineItemContainer.sAlbumCoverCtrl.panelNewMsgHint.get_Children().get_Count() == 0)
			{
				SnsNewMsgHintCtrl instance = SnsNewMsgHintCtrl.GetInstance();
				TimelineItemContainer.sAlbumCoverCtrl.panelNewMsgHint.get_Children().Add(instance);
			}
			SnsNewMsgHintCtrl.Refresh();
			TimelineItemContainer.sAlbumCoverCtrl.panelNewMsgHint.set_Visibility(0);
			TimelineItemContainer.sAlbumCoverCtrl.panelSendFailed.set_Visibility(0);
			if (TimelineItemContainer.sAlbumCoverCtrl.panelSendFailed.get_Children().get_Count() == 0)
			{
				TimelineItemContainer.sAlbumCoverCtrl.panelSendFailed.get_Children().Add(SendFailedHintsPanel.GetInstance(true));
			}
			SendFailedHintsPanel instance2 = SendFailedHintsPanel.GetInstance(false);
			if (TimelineItemContainer.sAlbumCoverCtrl.panelSendFailed != instance2.get_Parent())
			{
				TimelineItemContainer.sAlbumCoverCtrl.panelSendFailed.get_Children().Add(SendFailedHintsPanel.GetInstance(true));
			}
			if (TimelineItemContainer.sAlbumCoverCtrl.get_Parent() != null && TimelineItemContainer.sAlbumCoverCtrl.get_Parent() == this.gridContainer)
			{
				return;
			}
			this.removeCtrl();
			if (TimelineItemContainer.sAlbumCoverCtrl.get_Parent() != null)
			{
				ToolFunction.RemoveCtrlParent(TimelineItemContainer.sAlbumCoverCtrl);
			}
			this.mNTimeLineItemId = -2;
			this.gridContainer.get_Children().Add(TimelineItemContainer.sAlbumCoverCtrl);
		}
		private static void sAlbumCoverCtrl_Unloaded(object sender, RoutedEventArgs e)
		{
			TimelineItemContainer.sBIsAlblumCoverLoaded = false;
		}
		private static void sAlbumCoverCtrl_Loaded(object sender, RoutedEventArgs e)
		{
			TimelineItemContainer.sBIsAlblumCoverLoaded = true;
		}
		private void removeCtrl()
		{
			if (this.mNTimeLineItemId >= 0)
			{
				TimelineItem.PushOneItem(this.mNTimeLineItemId);
				this.gridContainer.get_Children().Clear();
				this.mNTimeLineItemId = -1;
				return;
			}
			this.gridContainer.get_Children().Clear();
		}
		public void UnLinked()
		{
			if (this.mNTimeLineItemId < 0)
			{
				this.gridContainer.get_Children().Clear();
			}
		}
		public void ClearMemory()
		{
			this.ItemObject = null;
			this.removeCtrl();
		}
		private void getCtrlItem()
		{
			lock (this.mObjectLocker)
			{
				if (this.gridContainer.get_Children().get_Count() != 1 || !(this.gridContainer.get_Children().get_Item(0) is TimelineItem))
				{
					this.gridContainer.get_Children().Clear();
					TimelineItem oneItem = TimelineItem.GetOneItem(null);
					this.mNTimeLineItemId = oneItem.mNItemId;
					this.gridContainer.get_Children().Add(oneItem);
				}
			}
		}
		private void setCtrlItem()
		{
			lock (this.mObjectLocker)
			{
				if (this.gridContainer.get_Children().get_Count() != 1 || this.mNTimeLineItemId == -2)
				{
					this.getCtrlItem();
				}
				if (this.gridContainer.get_Children().get_Count() == 1)
				{
					if (!(this.gridContainer.get_Children().get_Item(0) is TimelineItem))
					{
						this.getCtrlItem();
					}
					TimelineItem timelineItem = this.gridContainer.get_Children().get_Item(0) as TimelineItem;
					timelineItem.ItemObject = this.ItemObject;
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Circle/UI/TimelineItemContainer.xaml", 2));
			this.gridContainer = (Grid)base.FindName("gridContainer");
		}
	}
}
